package view_manage

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/go-admin-team/go-admin-core/sdk/api"
	_ "github.com/go-admin-team/go-admin-core/sdk/pkg/response"
	"vrcm/src/vrcm/app/admin/models/view"
	"vrcm/src/vrcm/app/admin/service/dto/view_manage"
	"vrcm/src/vrcm/app/admin/service/view_manage"
	"vrcm/src/vrcm/common/apis"

	"vrcm/src/vrcm/common/actions"
)

type ViewFit struct {
	api.Api
}

// GetPage 获取ViewFit列表
// @Summary 获取ViewFit列表
// @Description 获取ViewFit列表
// @Tags ViewFit
// @Param pageSize query int false "页条数"
// @Param pageIndex query int false "页码"
// @Success 200 {object} response.Response{data=response.Page{list=[]models.ViewFit}} "{"code": 200, "data": [...]}"
// @Router /api/v1/view-fit [get]
// @Security Bearer
func (e ViewFit) GetPage(c *gin.Context) {
	req := view_manage.ViewFitGetPageReq{}
	s := service.ViewFit{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	p := actions.GetPermissionFromContext(c)
	list := make([]view.ViewFit, 0)
	var count int64

	err = s.GetPage(&req, p, &list, &count)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取ViewFit失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}

// Get 获取ViewFit
// @Summary 获取ViewFit
// @Description 获取ViewFit
// @Tags ViewFit
// @Param id path int false "id"
// @Success 200 {object} response.Response{data=models.ViewFit} "{"code": 200, "data": [...]}"
// @Router /api/v1/view-fit/{id} [get]
// @Security Bearer
func (e ViewFit) Get(c *gin.Context) {
	req := view_manage.ViewFitGetReq{}
	s := service.ViewFit{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	var object view.ViewFit

	err = s.Get(&req, &object)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取ViewFit失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.OK(object, "查询成功")
}

// Insert 创建ViewFit
// @Summary 创建ViewFit
// @Description 创建ViewFit
// @Tags ViewFit
// @Accept application/json
// @Product application/json
// @Param data body dto.ViewFitInsertReq true "data"
// @Success 200 {object} response.Response	"{"code": 200, "message": "添加成功"}"
// @Router /api/v1/view-fit [post]
// @Security Bearer
func (e ViewFit) Insert(c *gin.Context) {
	req := view_manage.ViewFitInsertReq{}
	s := service.ViewFit{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	// 设置创建人
	//req.SetCreateBy(user.GetUserId(c))
	err = s.Insert(&req)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("创建ViewFit失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.OK(req.GetId(), "创建成功")
}

// Update 修改ViewFit
// @Summary 修改ViewFit
// @Description 修改ViewFit
// @Tags ViewFit
// @Accept application/json
// @Product application/json
// @Param id path int true "id"
// @Param data body dto.ViewFitUpdateReq true "body"
// @Success 200 {object} response.Response	"{"code": 200, "message": "修改成功"}"
// @Router /api/v1/view-fit/{id} [put]
// @Security Bearer
func (e ViewFit) Update(c *gin.Context) {
	req := view_manage.ViewFitUpdateReq{}
	s := service.ViewFit{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	if err != nil {
		e.Error(500, err, fmt.Sprintf("修改ViewFit失败，\r\n失败信息 %s", err.Error()))
		return
	}
	p := actions.GetPermissionFromContext(c)

	err = s.Update(&req, p)
	if err != nil {
		if err.Error() == string(apis.ImageError.Code) {
			e.Error(apis.ImageError.Code, err, "修改页面装修失败: 图片地址不正确")
		} else {
			e.Error(500, err, fmt.Sprintf("修改ViewFit失败，\r\n失败信息 %s", err.Error()))
		}

		return
	}
	e.OK(req.GetId(), "修改成功")
}

// Delete 删除ViewFit
// @Summary 删除ViewFit
// @Description 删除ViewFit
// @Tags ViewFit
// @Param data body dto.ViewFitDeleteReq true "body"
// @Success 200 {object} response.Response	"{"code": 200, "message": "删除成功"}"
// @Router /api/v1/view-fit [delete]
// @Security Bearer
func (e ViewFit) Delete(c *gin.Context) {
	s := service.ViewFit{}
	req := view_manage.ViewFitDeleteReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	// req.SetUpdateBy(user.GetUserId(c))
	p := actions.GetPermissionFromContext(c)

	err = s.Remove(&req, p)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("删除ViewFit失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(req.GetId(), "删除成功")
}

// Get 获取ViewFit通过bind_id
// @Summary 获取ViewFit
// @Description 获取ViewFit
// @Tags ViewFit
// @Param id path int false "id"
// @Success 200 {object} response.Response{data=models.ViewFit} "{"code": 200, "data": [...]}"
// @Router /api/v1/view-fit/GetByBindId post
// @Security Bearer
func (e ViewFit) GetByBindId(c *gin.Context) {
	req := view_manage.ViewFitGetReq{}
	s := service.ViewFit{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	if err != nil {
		e.Error(500, err, fmt.Sprintf("查询ViewFit失败，\r\n失败信息 %s", err.Error()))
		return
	}
	var object view.ViewFit

	s.Get(&req, &object)

	e.OK(object, "查询成功")
}

func (e ViewFit) GetInfoByBindId(c *gin.Context) {
	req := view_manage.ViewReq{}
	s := service.ViewFit{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.GetInfoByBindId(&req)
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	e.OK(data, "查询成功")
}
